package com.lu.pojo.thread;

import java.util.concurrent.*;

/**
 * @author luzhenfang
 * @date 2020 05 23
 * @description callable接口 需要返回值类型
 * 重写call 方法
 * 需要抛出异常
 */
public class TestCallable implements Callable<Boolean> {

    private  String name;

    public TestCallable(String name) {
        this.name =name;
    }

    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println(this.name+" 在写bug:"+i);
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("张三");
        TestCallable t2 = new TestCallable("李四");
        TestCallable t3 = new TestCallable("王五");
        // 创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(3);

        // 提交执行
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        // 获取结果
        Boolean rs1 = r1.get();
        Boolean rs2 = r2.get();
        Boolean rs3 = r3.get();

        // 关闭服务
        ser.shutdown();

        System.out.println("----shut down -----");

        System.out.println(rs1);
        System.out.println(rs2);
        System.out.println(rs3);
    }
}
